import {clientRequest} from "~/utils/backend_request";
import {type ParamsWithSearch} from "~/api/common";
import type {Dayjs} from "dayjs";
import dayjs from "dayjs";

export function ApiGetMemberList(params: ParamsWithSearch = {page:1, limit: 10, search:""}) {
    return clientRequest("/api/backend/member", "get", params)
}

export interface MemberData{
    member_id: Number | null
    name: string
    number: string
    phone_number: string
    nationality: string
    birthday: Dayjs
    member_pic: Blob | string | null | undefined | File
    passport_picture: Blob | string | null | undefined | File
    visa_period: [string, string]| [Dayjs, Dayjs]
    passport_no: string
    passport_sign_location: string
    passport_period: [string, string]| [Dayjs, Dayjs]
    gender: 0|1
    description: string
    visa_issue_at: string|null
    visa_expires_at: string|null
    birthday_string: string|null
    passport_issue_at: string|null
    passport_expires_at: string|null
}

export interface VisaAndPassportIssueData {
    member_id: Number | null
    passport_picture: Blob | string | null | undefined | File
    visa_period: [string, string]| [Dayjs, Dayjs]
    passport_no: string
    passport_sign_location: string
    passport_period: [string, string]| [Dayjs, Dayjs]
    visa_issue_at: string|null
    visa_expires_at: string|null
    passport_issue_at: string|null
    passport_expires_at: string|null
}

export interface CardIssueData{
    number: string,
    member_id: Number | null
    card_no: string
}
export interface EnabledDisabledData{
    member_id: Number | null
    status: 0|1
}
function dayjsConversion(params: VisaAndPassportIssueData){
    params["visa_issue_at"] = dayjs(params.visa_period[0]).format("YYYY-MM-DD")
    params["visa_expires_at"] = dayjs(params.visa_period[1]).format("YYYY-MM-DD")
    params["passport_issue_at"] = dayjs(params.passport_period[0]).format("YYYY-MM-DD")
    params["passport_expires_at"] = dayjs(params.passport_period[1]).format("YYYY-MM-DD")
}
export function ApiCreateMember(params :MemberData) {
    params["birthday_string"] = dayjs(params.birthday).format("YYYY-MM-DD")
    dayjsConversion(params)
    return clientRequest("/api/backend/member/create", "post", params)
}

export function ApiIssueCard(params :CardIssueData) {
    return clientRequest("/api/backend/member/sign_card", "post", params)
}

export function ApiUpdateMember(params :MemberData) {
    params["birthday_string"] = dayjs(params.birthday).format("YYYY-MM-DD")
    dayjsConversion(params);
    return clientRequest("/api/backend/member/update", "post", params)
}

export function ApiDeleteMember(member_id:Number) {
    return clientRequest("/api/backend/member/delete", "delete", {member_id})
}

export function ApiFreezeMember(member_id:Number) {
    return clientRequest("/api/backend/member/freeze", "PUT", {member_id, status :0})
}
export function ApiUnFreezeMember(member_id:Number) {
    return clientRequest("/api/backend/member/freeze", "PUT", {member_id, status :1})
}

export function ApiUpdateMemberVisaInfo(params :VisaAndPassportIssueData) {
    dayjsConversion(params);
    let result: VisaAndPassportIssueData = {
        member_id: params.member_id,
        passport_expires_at : params.passport_expires_at,
        passport_issue_at : params.passport_issue_at,
        visa_expires_at: params.visa_expires_at,
        visa_issue_at: params.visa_issue_at,
        passport_sign_location: params.passport_sign_location,
        visa_period: params.visa_period,
        passport_picture: params.passport_picture,
        passport_no: params.passport_no,
        passport_period: params.passport_period
    }

    return clientRequest("/api/backend/member/update", "post", result)
}

export function ApiUpdateVisaInfo(params: VisaAndPassportIssueData) {
    dayjsConversion(params)
    return clientRequest("/api/backend/member/update_visa_info", "post", params)
}
